package com.castle.fortress.admin.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.dto.MemberGroupRelationDto;
import com.castle.fortress.admin.member.entity.MemberGroupRelationEntity;
import com.castle.fortress.admin.member.mapper.MemberGroupRelationMapper;
import com.castle.fortress.admin.member.service.MemberGroupRelationService;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 会员分组关联关系表 服务实现类
 *
 * @author Mgg
 * @since 2021-12-03
 */
@Service
public class MemberGroupRelationServiceImpl extends ServiceImpl<MemberGroupRelationMapper, MemberGroupRelationEntity> implements MemberGroupRelationService {
    /**
    * 获取查询条件
    * @param memberGroupRelationDto
    * @return
    */
    public QueryWrapper<MemberGroupRelationEntity> getWrapper(MemberGroupRelationDto memberGroupRelationDto){
        QueryWrapper<MemberGroupRelationEntity> wrapper= new QueryWrapper();
        if(memberGroupRelationDto != null){
            MemberGroupRelationEntity memberGroupRelationEntity = ConvertUtil.transformObj(memberGroupRelationDto,MemberGroupRelationEntity.class);
            wrapper.like(memberGroupRelationEntity.getMemberId() != null,"member_id",memberGroupRelationEntity.getMemberId());
            wrapper.like(memberGroupRelationEntity.getGroupId() != null,"group_id",memberGroupRelationEntity.getGroupId());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberGroupRelationDto> pageMemberGroupRelation(Page<MemberGroupRelationDto> page, MemberGroupRelationDto memberGroupRelationDto) {
        QueryWrapper<MemberGroupRelationEntity> wrapper = getWrapper(memberGroupRelationDto);
        Page<MemberGroupRelationEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberGroupRelationEntity> memberGroupRelationPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberGroupRelationDto> pageDto = new Page(memberGroupRelationPage.getCurrent(), memberGroupRelationPage.getSize(),memberGroupRelationPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(memberGroupRelationPage.getRecords(),MemberGroupRelationDto.class));
        return pageDto;
    }


    @Override
    public List<MemberGroupRelationDto> listMemberGroupRelation(MemberGroupRelationDto memberGroupRelationDto){
        QueryWrapper<MemberGroupRelationEntity> wrapper = getWrapper(memberGroupRelationDto);
        List<MemberGroupRelationEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,MemberGroupRelationDto.class);
    }
}

